package com.lkd.service.impl;

import com.lkd.entity.Goods;
import com.lkd.entity.Users;
import com.lkd.mapper.GoodsMapper;
import com.lkd.service.IGoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<Goods> queryAll() {
        return goodsMapper.queryAll();
    }

    @Override
    public Goods checkGood(Goods goods) {
        return goodsMapper.checkGood(goods);
    }

    @Override
    public int addGood(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }

    @Override
    public int deleteGood(Goods goods) {
        return goodsMapper.deleteByPrimaryKey(Long.valueOf(goods.getGoodsId()));
    }

    @Override
    public int updateGood(Goods goods) {
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public int downInventory(Integer goodsId) {
        return goodsMapper.downInventory(goodsId);
    }

    @Override
    @HystrixCommand(fallbackMethod = "queryGoodsById_fallback",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
            })
    public Goods queryGoodsById(Long goodsId) {
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        if(goods == null) {
            throw new RuntimeException("查询不到商品信息！");
        }
        return goods;
    }

    //如果出现异常 采取Hystrix的备选方案
    public Goods queryGoodsById_fallback(Long goodsId){
        return new Goods(goodsId.intValue(),"服务熔断-暂时查询不到商品信息");
    }


}
